package com.domain.assembler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.DO.FlightClass;
import com.domain.DO.FlightInfo;
import com.domain.DO.FlightOrder;
import com.domain.DTO.FlightOrderDto;
import com.mapper.FlightClassMapper;
import com.mapper.FlightInfoMapper;
import com.mapper.FlightOrderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.domain.factory.FlightOrderFactory.FLIGHT_ORDER_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/11/11 18:08
 */
@Component
@RequiredArgsConstructor
public class FlightOrderAssembler {
    private final FlightOrderMapper flightOrderMapper;
    private final FlightClassMapper flightClassMapper;
    private final FlightInfoMapper flightInfoMapper;

    public List<FlightOrderDto> getFlightOrderInfoByUserId(Integer uid) {
        //1.获取当前用户下的订单
        List<FlightOrder> flightOrderList = flightOrderMapper.selectByMap(Map.of("user_id", uid));
        return flightOrderList.stream().map(flightOrder -> {
            //2.查询出每个订单对应的班次信息
            FlightClass flightClass = flightClassMapper.selectOne(new QueryWrapper<FlightClass>().eq("id", flightOrder.getFlightClassId()));
            //3.查询出当前班次对应的航班信息
            FlightInfo flightInfo = flightInfoMapper.selectOne(new QueryWrapper<FlightInfo>().eq("id", flightClass.getFlightId()));
            //4.拼接上述信息得到最终的订单信息
            return FLIGHT_ORDER_CONVERT.toFlightOrderDto(flightOrder,flightClass,flightInfo);
        }).collect(Collectors.toList());
    }

    public FlightOrderDto createUserOrderInfo(FlightClass flightClass, FlightOrder flightOrder) {
          //1.查询出航班信息
         FlightInfo flightInfo = flightInfoMapper.selectById(flightClass.getFlightId());
          //2.拼接所有信息
         return FLIGHT_ORDER_CONVERT.toFlightOrderDto(flightOrder,flightClass,flightInfo);
    }
}
